﻿(function ($) {
    DataEntity = function (options) {
        this.options = $.extend({ map: {} }, options);
    };

    DataEntity.VERSION = "1.0";
    DataEntity.Author = "Argo Zhang";
    DataEntity.Email = "argo@163.com";

    DataEntity.prototype = {
        load: function (value) {
            for (name in this.options.map) {
                var ctl = $("#" + this.options.map[name]);
                if (ctl.hasClass('select')) ctl.selectval(value[name]);
                else if (ctl.hasClass('radio-group')) {
                    var radio = ctl.attr('data-link-control');
                    $('input[name="' + radio + '"][value="' + value[name] + '"]').prop('checked', true);
                    ctl.val(value[name]);
                } else if (ctl.hasClass('checkbox-group')) {
                    var checkbox = ctl.attr('data-link-control');
                    if (typeof (value[name]) == "string") {
                        $.map(value[name].split(","), function (element, index) {
                            $('input[name="' + checkbox + '"][value="' + element + '"]').prop('checked', true);
                        });
                    } else {
                        $('input[name="' + checkbox + '"][value="' + value[name] + '"]').prop('checked', true);
                    }
                    ctl.val(value[name]);
                } else if (ctl.hasClass('tagsinput')) {
                    ctl.tagsinput('add', value[name]);
                }
                else ctl.val(value[name]);
            }
        },
        reset: function () {
            for (name in this.options.map) {
                var ctl = $("#" + this.options.map[name]);
                var dv = ctl.attr("data-default-val");
                if (dv === undefined) dv = "";
                if (ctl.hasClass('select')) ctl.selectval(dv);
                else if (ctl.hasClass('radio-group')) {
                    ctl.val(dv);
                    var radio = ctl.attr('data-link-control');
                    $('input[name="' + radio + '"][value="' + dv + '"]').prop('checked', true);
                } else if (ctl.hasClass('checkbox-group')) {
                    ctl.val(dv);
                    var checkbox = ctl.attr('data-link-control');
                    $('input[name="' + checkbox + '"][value="' + dv + '"]').prop('checked', true);
                }
                else ctl.val(dv);
            }
        },
        get: function () {
            var target = {};
            for (name in this.options.map) {
                var ctl = $("#" + this.options.map[name]);
                if (ctl.hasClass('select')) {
                    target[name] = ctl.selectval() == "" ? ctl.attr("data-default-val") : ctl.selectval();
                }
                else if (ctl.hasClass('radio-group')) {
                    var radio = ctl.attr('data-link-control');
                    ctl.val($('input[name="' + radio + '"]:checked').val());
                    target[name] = ctl.val();
                } else if (ctl.hasClass('data-json')) {
                    target[name] = ctl.data('data-json');
                } else if (ctl.hasClass('checkbox-group')) {
                    var checkbox = ctl.attr('data-link-control');
                    ctl.val($('input[name="' + checkbox + '"]:checked').map(function () {
                        return $(this).val();
                    }).get().join(','));
                    target[name] = ctl.val();
                }
                else if (ctl.hasClass('imgUrl')) {
                    var imgUrl = ctl.data('imgUrl');
                    target[name] = imgUrl;
                } else if (ctl.hasClass('img-responsive')) {
                    target[name] = ctl.attr('data-file');
                }
                else {
                    var dv = ctl.attr('data-default-val');
                    if (dv !== undefined && ctl.val().trim() === "") target[name] = dv;
                    else target[name] = ctl.val();
                }
            }
            return target;
        }
    }
}(jQuery));